import { required } from './func'

const regs = {
  mobile: /^1\d{10}$/,
  passwd: /^[0-9A-Za-z]{6,10}$/,
  loginName: /^[0-9A-Za-z]{6,12}$/,
  carId: /^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$/,
  name: /^[\u4E00-\u9FA5]{1,6}$/, // 中文名字 ~~~~
  serialNo: /^[0-9A-Za-z]{1,20}$/
}

class Validator {
  is ({ type = required(), input = '' }) {
    const reg = regs[type]
    if (!reg) {
      throw new Error(`还没有${type}的正则表达式哦！`)
    } else {
      if (input === null) {
        return false
      }
      return reg.test(input)
    }
  }

  not ({ type = required(), input = '' }) {
    return !this.is({ type, input })
  }
}

const instance = new Validator()
export default instance
